using System.Collections.Generic;
using static IQIGame.Onigao.GamePlay.Editors.EntityGenConfigAsset;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class EntityAnimModifyCollect
    {
        public class EntityAnimActiveResetState
        {
            public EntityEffectStateBind stateBindInfo;
            public readonly List<int> objIndexs = new List<int>();

            public EntityAnimActiveResetState(EntityEffectStateBind stateBindInfo)
            {
                this.stateBindInfo = stateBindInfo;
            }
        }

        public int effectBindCount { private set; get; }

        /// <summary>
        /// 离开状态需要关闭的Object
        /// </summary>
        public readonly Dictionary<string, EntityAnimActiveResetState> stateGoAcitveReset = new Dictionary<string, EntityAnimActiveResetState>();

        /// <summary>
        /// 动画资源相关
        /// </summary>
        public readonly Dictionary<string, EntityAnimConfig> animResConfigs = new Dictionary<string, EntityAnimConfig>();

        /// <summary>
        /// VFX
        /// </summary>
        public readonly Dictionary<string, AniamtorCustomVFX> vfxConfigs = new();

        public static EntityAnimModifyCollect Create(EntityGenConfigAsset entityConfigAsset, ArtModeGenType genType)
        {
            EntityAnimModifyCollect entityAnimModifyCollect = new EntityAnimModifyCollect();
            var rEffectbindList = new List<EntityGenConfigAsset.EntityEffectBind>(entityConfigAsset.effectBinds);
            rEffectbindList.RemoveAll(x => ((int)x.genType & (1 << (int)genType)) <= 0);
            entityAnimModifyCollect.effectBindCount = rEffectbindList.Count;
            int startIndex = 0;
            for (int i = 0; i < rEffectbindList.Count; i++)
            {
                var e = rEffectbindList[i];
                if (e.effectAnimStateBinds != null && e.effectAnimStateBinds.Count > 0)
                {
                    foreach (var stateBind in e.effectAnimStateBinds)
                    {
                        string stateName = stateBind.stateName;
                        if (!entityAnimModifyCollect.stateGoAcitveReset.TryGetValue(stateName, out var entityAnimActiveReset))
                        {
                            entityAnimActiveReset = new EntityAnimActiveResetState(stateBind);
                            entityAnimModifyCollect.stateGoAcitveReset[stateName] = entityAnimActiveReset;
                        }
                        entityAnimActiveReset.objIndexs.Add(i + startIndex);
                    }
                }
            }

            startIndex += rEffectbindList.Count;

            for (int i = 0; i < entityConfigAsset.objectProcesses.Count; i++)
            {
                var e = entityConfigAsset.objectProcesses[i];
                if (e.effectAnimStateBinds != null && e.effectAnimStateBinds.Count > 0)
                {
                    foreach (var stateBind in e.effectAnimStateBinds)
                    {
                        string stateName = stateBind.stateName;
                        if (!entityAnimModifyCollect.stateGoAcitveReset.TryGetValue(stateName, out var entityAnimActiveReset))
                        {
                            entityAnimActiveReset = new EntityAnimActiveResetState(stateBind);
                            entityAnimModifyCollect.stateGoAcitveReset[stateName] = entityAnimActiveReset;
                        }
                        entityAnimActiveReset.objIndexs.Add(i + startIndex);
                    }
                }
            }

            foreach (var anim in entityConfigAsset.entityAnimConfigs)
            {
                entityAnimModifyCollect.animResConfigs.TryAdd(anim.animtionName, anim);
            }

            foreach (var vfx in entityConfigAsset.customVFXStates)
            {
                if (!string.IsNullOrEmpty(vfx.stateName) && !string.IsNullOrEmpty(vfx.vfxGroup))
                {
                    entityAnimModifyCollect.vfxConfigs.TryAdd(vfx.stateName, vfx);
                }
            }
            return entityAnimModifyCollect;
        }


    }
}
